package cn.sangyy.org.service.impl;

import cn.sangyy.basic.utils.PageList;
import cn.sangyy.org.domain.Department;
import cn.sangyy.org.domain.Employee;
import cn.sangyy.org.mapper.DepartmentMapper;
import cn.sangyy.org.query.DepQuery;
import cn.sangyy.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author SangYY
 * @date: 2023/04/23/16:06
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public boolean insert(Department department) {
        department.setCreateTime(new Date());
        Department parent = department.getParent();
        Employee manager = department.getManager();
        if(manager.getId() != null){
            department.setManagerId(manager.getId());
        }
        int insert = departmentMapper.insert(department);
        if(parent != null){
            if(parent.getId() != null){
                department.setParentId(parent.getId());
                Department depById = departmentMapper.getDepById(parent.getId());
                department.setPath(depById.getPath()+"/"+department.getId());
            }else {
                department.setPath("/"+department.getId());
            }
        }else {
            department.setPath("/"+department.getId());
        }
        departmentMapper.update(department);
        return insert == 1;
    }

    @Override
    @Transactional
    public boolean deleteById(Serializable id) {
        int delete = departmentMapper.deleteById(id);
        return delete == 1;
    }

    @Override
    @Transactional
    public boolean update(Department department) {
        department.setUpdateTime(new Date());
        int update = departmentMapper.update(department);
        return update == 1;
    }

    @Override
    public Department getDepById(Serializable id) {
        return departmentMapper.getDepById(id);
    }

    @Override
    public List<Department> getAllDep() {
        return departmentMapper.getAllDep();
    }

    @Override
    public PageList<Department> getDepPageByKeyword(DepQuery depQuery) {
        String keyword = depQuery.getKeyword();
        Long totals = departmentMapper.getTotals(keyword);
        Integer page = depQuery.getPage();
        Integer rows1 = depQuery.getRows();
        if((page == null || page < 1) || (rows1 == null)){
            throw new RuntimeException("数据异常");
        }
        List<Department> rows = departmentMapper.getRows(depQuery);
        return new PageList<>(totals,rows);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        if(ids.size() < 1 || ids == null){
            throw new RuntimeException("数据异常");
        }else {
            departmentMapper.batchDelete(ids);
        }
    }

    @Override
    public List<Department> depTree() {
        return departmentMapper.depTree();
    }
}
